package com.juyu.miliao.api.rabbitMQ;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.service.MakeFriendsService;
import com.juyu.miliao.common.constant.MakeFriendsMqKeyInfo;
import com.juyu.miliao.common.domain.dto.MakeFriendsMqUserVipTypeDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author xuanqicheng
 * @create 2019-09-11:45
 */
@Component
@Slf4j
public class MakeFriendsMq {
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private MakeFriendsService makeFriendsService;


    public void sendUserVipType(MakeFriendsMqUserVipTypeDTO makeFriendsMqUserVipTypeDTO) {
        amqpTemplate.convertAndSend(MakeFriendsMqKeyInfo.MAKE_FRIENDS_EXCHANGE,
                MakeFriendsMqKeyInfo.MAKE_FRIENDS_USER_VIP_TYPE_ROUTING,
                JSONObject.toJSONString(makeFriendsMqUserVipTypeDTO));
    }


    @RabbitListener(bindings = @QueueBinding(value = @Queue(MakeFriendsMqKeyInfo.MAKE_FRIENDS_USER_VIP_TYPE_QUEUE),
            exchange = @Exchange(MakeFriendsMqKeyInfo.MAKE_FRIENDS_EXCHANGE),
            key = MakeFriendsMqKeyInfo.MAKE_FRIENDS_USER_VIP_TYPE_ROUTING))
    public void getUserVipTypeMessage(String message) {
        if (StringUtils.isNotBlank(message)) {
            try {
                MakeFriendsMqUserVipTypeDTO makeFriendsMqUserVipTypeDTO = JSONObject.parseObject(message,
                        MakeFriendsMqUserVipTypeDTO.class);
                makeFriendsService.getUserVipType(makeFriendsMqUserVipTypeDTO);
            } catch (Exception ex) {
                log.error("同城处理vip状态错误："+ex.getMessage());
            }
        }
    }
}